//
//  ReserveViewController.m
//  Reservation
//
//  Created by Wai Man Chan on 4/22/14.
//  Copyright (c) 2014 CHAN, Wai Man. All rights reserved.
//

#import "ReserveViewController.h"
#import "LoginModel.h"

@interface ReserveViewController ()

@end

@implementation ReserveViewController
@synthesize restaurant, datePicker, partySizeField;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"Reserve";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneReserve:)];
}

- (IBAction)doneReserve:(id)sender {
    LoginModel *model = [LoginModel shareModel];
    NSDateFormatter *df = [NSDateFormatter new];
    df.dateFormat = @"yyyy-MM-dd%20HH:mm:ss";
    NSString *date = [df stringFromDate:datePicker.date];
    NSString *address = [NSString stringWithFormat:@"http://happywaiman.dlinkddns.com/Reserve/User/Appoinment.php?action=create&time=%@&uid=_uid&cid=_cid&sid=%lld", date, restaurant.sid];
    [model queryWithAddress:address withQuery:^(NSData *data) {
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:NULL];
        if (((NSNumber *)dictionary[@"state"]).boolValue) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reserveration" message:@"We make you an appoinment" delegate:NULL cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reserveration Error" message:@"We can't made you an appoinment" delegate:NULL cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
    } cancel:^{
    }];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)clickOutside:(id)sender {
    [partySizeField resignFirstResponder];
}

@end
